//
//  XMAlbumScrollView.m
//  XiaoMiao
//
//  Created by 23 on 2018/1/29.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumScrollView.h"

#define MaxSCale 5.0 //最大缩放比例
#define MinScale 0.5 //最小缩放比例

@interface XMAlbumScrollView () <UIScrollViewDelegate>

/**图片*/
@property(nonatomic,weak) UIImageView *imageView;

/**是否是更新的*/
@property(nonatomic,assign) BOOL firstLoad;

/**flag*/
@property(nonatomic,assign) BOOL flag;

@end

@implementation XMAlbumScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.firstLoad = YES;
        [self setupUI];
    }
    return self;
}



#pragma mark - UI
- (void)setupUI
{
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//    [self addSubview:imageView];
//    self.imageView = imageView;
//
//    self.delegate = self;
//    self.minimumZoomScale = MinScale;
//    self.maximumZoomScale = MaxSCale;
//
//    self.bouncesZoom = NO;
//    [self setZoomScale:1.0];
    
    self.bounces = NO;
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (self.isStandard && self.firstLoad) {
        self.imageView.center = self.center;
        self.firstLoad = NO;
    }
    
    if (self.flag) {
        self.imageView.frame = self.bounds;
    }
    
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
{
    self.flag = NO;
    [self layoutIfNeeded];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
    CGRect frame = self.imageView.frame;
    
    frame.origin.y = (scrollView.frame.size.height - self.imageView.frame.size.height) > 0 ? (scrollView.frame.size.height - self.imageView.frame.size.height) * 0.5 : 0;
    frame.origin.x = (scrollView.frame.size.width - self.imageView.frame.size.width) > 0 ? (scrollView.frame.size.width - self.imageView.frame.size.width) * 0.5 : 0;
    self.imageView.frame = frame;
    
    scrollView.contentSize = CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height);
    
    
    if (self.callback) {
        self.callback(scrollView.zoomScale, scrollView.contentOffset);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.isCompare && self.callback) {
        self.offsetCallback(scrollView.contentOffset, self);
    }else if(self.callback) {
        self.callback(scrollView.zoomScale, scrollView.contentOffset);
    }
}


- (void)setImageModel:(XMAlbumScanModel *)imageModel
{
    _imageModel = imageModel;
    self.flag = YES;
    if (self.imageView) {
        [self.imageView removeFromSuperview];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:imageView];
    self.imageView = imageView;
    
    self.delegate = self;
    self.minimumZoomScale = MinScale;
    self.maximumZoomScale = MaxSCale;
    self.bouncesZoom = NO;
    if (self.isScanner) {
        self.bouncesZoom = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    [self setZoomScale:1.0];
    if (self.isScanner) {
        [self setZoomScale:1.0];
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
    }
    
    
    
    WEAKSELF
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:imageModel.imageUrl] placeholderImage:imageModel.image completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        //压缩图片
        if (weakSelf.isScanner) {
            
            
            UIImage *resultImg = [weakSelf imageCompressWithSimple:image];
            
            //            CGFloat height = kScreenWidth * image.size.height / image.size.width;
            //            UIImage *resultImg = [weakSelf scaleToSize:image size:CGSizeMake(kScreenWidth, height)];
            
            weakSelf.contentSize = resultImg.size;
            weakSelf.imageView.image = resultImg;
            [weakSelf.imageView sizeToFit];
            [weakSelf setZoomScale:1.0];
            
            if (weakSelf.isCompare) {
                if (weakSelf.loadCallback) {
                    weakSelf.loadCallback(0);
                }
            }
            
        }
        
        else{
            weakSelf.contentSize = image.size;
            [weakSelf.imageView sizeToFit];
//            if (imageModel.noScale) {
//                [weakSelf setZoomScale:1.0];
//            }else{
//                [weakSelf setZoomScale:0.5];
//            }
            [weakSelf setZoomScale:1.0];

        }
        
        
        
    }];
    
}


/**
 *  上传原图
 *
 *  @param image 传过来的图片
 *
 *  @return 返回的图片
 */
- (UIImage*)imageCompressWithSimple:(UIImage*)image{
    CGSize size = image.size;
    CGFloat scale = 1.0;
    //TODO:KScreenWidth屏幕宽
    if (size.width > kScreenWidth || size.height > kScreenHeight) {
        if (size.width > size.height) {
            scale = kScreenWidth / size.width;
        }else {
            scale = kScreenHeight / size.height;
        }
    }
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat scaledWidth = width * scale;
    CGFloat scaledHeight = height * scale;
    CGSize secSize =CGSizeMake(scaledWidth, scaledHeight);
    //TODO:设置新图片的宽高
    UIGraphicsBeginImageContext(secSize); // this will crop
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}



- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0,0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)setZoom:(CGFloat)zoom
{
    _zoom = zoom;
    
    [self setZoomScale:zoom animated:YES];
}

- (void)setOffset:(CGPoint)offset {
    _offset = offset;
    
    self.contentOffset = offset;
}

@end
